﻿using System.IO;

namespace maranov.SymlinkManager.FilesystemLinks
{
    /// <summary>
    /// Filesystem object location.
    /// </summary>
    public class Location : ILocation
    {
        /// <summary>
        /// Filesystem object location.
        /// </summary>
        /// <param name="fullname">Full (absolute) object name.</param>
        public Location(string fullname)
        {
            Name = fullname;
        }

        public bool Exists
        {
            get
            {
                return (
                    File.Exists(Name) ||
                    Directory.Exists(Name));
            }
        }

        public string Name
        {
            get;
            private set;
        }

        public LocationType Type
        {
            get
            {
                // No caching, object can be removed/created/... any time.
                if (Exists)
                {
                    FileAttributes attributes = File.GetAttributes(Name);

                    if ((attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
                        return LocationType.SymbolicLink;
                    else if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
                        return LocationType.HardDirectory;
                    else
                        return LocationType.HardFile;
                }
                else
                {
                    return LocationType.None;
                }
            }
        }
    }
}
